package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhouwei
 * @date 2019-12-29 19:34
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    //定义服务接口
    @Reference
    private CheckItemService checkItemService;
    private Result result;

    //新增数据
    @PreAuthorize("hasAnyAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return  new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        //服务调用成功
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    //分页查询
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //替换所有空格,页面第一次加载，queryString为null，要先抓空指针异常
        try {
            String queryString = queryPageBean.getQueryString();
            if(queryString!=null&&queryString.length()!=0) {
                String s = queryString.replaceAll(" ", "");
                queryPageBean.setQueryString(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageResult pageResult=checkItemService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 删除按钮
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')") //权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            checkItemService.delete(id);
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    /**
     * 查找单个检查项
     */
     @RequestMapping("/findById")
    public Result findById(Integer id){
         Result result= checkItemService.findById(id);
         return result;
     }

    /**
     * 编辑方法
     */
    @PreAuthorize("hasAnyAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return  new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        //服务调用成功
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

    /**
     * 查询所有检查项
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try {
             List<CheckItem> list=checkItemService.findAll();
            return  new Result(true,MessageConstant.ADD_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //调用服务失败
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }


}
